import { http } from 'utils'

export async function saveProducts(payload) {
  const path = payload.id ? 'products/update' : 'products/save'
  try {
    const res = await http.post(path, payload)
    if (res.code === 1) {
      return res
    }
    throw res.info
  } catch (err) {
    throw err
  }
}

export async function getProducts(params) {
  try {
    const res = await http.get('products/list', params)
    if (res.code === 1) {
      return res.data
    }
    throw res.info
  } catch (err) {
    throw err
  }
}

export async function delProductById(id) {
  try {
    const res = await http.get('products/delete', { id })
    if (res.code === 1) {
      return res.data
    }
    throw res.info
  } catch (err) {
    throw err
  }
}

export async function getProductById(id) {
  try {
    const res = await http.get('products/info', { id })
    if (res.code === 1) {
      return res.data
    }
    throw res.info
  } catch (err) {
    throw err
  }
}
